package com.jt.ktdemo.ui.mian

import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import android.widget.Toast
import androidx.fragment.app.Fragment
import com.bumptech.glide.Glide
import com.jt.ktdemo.R
import com.jt.ktdemo.bean.PersonCallbackBean
import com.jt.ktdemo.http.HTTPAPI
import com.jt.ktdemo.ui.changePwd.ChangePwdActivity
import com.jt.ktdemo.ui.myinfo.MyInfoActivity
import com.jt.ktdemo.ui.register.RegisterActivity
import com.jt.ktdemo.ui.setting.SettingActivity
import com.jt.ktdemo.utils.GsonUtils
import com.jt.ktdemo.utils.LogUtil
import com.jt.ktdemo.utils.SpUtils
import com.lzy.okgo.callback.StringCallback
import kotlinx.android.synthetic.main.myinfo.*
import okhttp3.Call
import okhttp3.Response
import java.lang.Exception

class MyFragment : Fragment(), View.OnClickListener {

    private lateinit var rlModuleMyInfo:RelativeLayout
    private lateinit var rlModuleChangePwd:RelativeLayout
    private lateinit var rlModuleSeting:RelativeLayout
    private lateinit var textView26:TextView
    private lateinit var ivModule:ImageView

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        var view = inflaterView(inflater, container)
        if (view != null) {
            initView(view)
        }
        return view
    }

    private fun initView(view: View) {
        textView26 = view.findViewById(R.id.textView26)
        ivModule = view.findViewById(R.id.ivMdoule)
        rlModuleMyInfo = view.findViewById(R.id.rlModuleMyInfo)
        rlModuleChangePwd = view.findViewById(R.id.rlModuleChangePwd)
        rlModuleSeting = view.findViewById(R.id.rlModuleSetting)
        rlModuleMyInfo.setOnClickListener(this)
        rlModuleChangePwd.setOnClickListener(this)
        rlModuleSeting.setOnClickListener(this)

        var name = SpUtils["module_name", ""].toString()
        var email = SpUtils["module_email",""].toString()
        view.findViewById<TextView>(R.id.textView26).text = name


    }

    override fun onResume() {
        super.onResume()
        getPersion()
    }

    private fun inflaterView(inflater: LayoutInflater, container: ViewGroup?): View? {
        return inflater.inflate(R.layout.myinfo, container, false)
    }

    override fun onClick(v: View?) {
        if (v?.id == R.id.rlModuleSetting){
            startActivity(Intent(activity, SettingActivity().javaClass))
        }
    }


    /**
     * 获取个人信息
     */
    fun getPersion() {
        HTTPAPI.getPersion(object : StringCallback() {
            override fun onSuccess(t: String?, call: Call?, response: Response?) {
                LogUtil.e("个人信息-->$t")
                var bean : PersonCallbackBean = GsonUtils.parse(t.toString(), PersonCallbackBean::class.java)
                try {
                    if (!bean.isError) {
                        SpUtils.put("module_name",bean.rcUser?.realName ?: "")
                        SpUtils.put("module_email", bean.rcUser?.email!!)
                        SpUtils.put("module_phone",bean.rcUser?.mobileNo!!)
                        textView26.setText(bean.rcUser?.username)
                    }
                    var uid = SpUtils["module_uid", ""].toString()
                    if (!TextUtils.isEmpty(uid)) {
                        activity?.let {
                            Glide.with(it)
                                .load("http://ec2-13-59-112-96.us-east-2.compute.amazonaws.com:8080/face/home/ubuntu/face/" + uid + ".png")
                                .into(ivModule)
                        }
                    }
                }catch (e:Exception){
                    Toast.makeText(activity,"联网返回值异常-->$t",Toast.LENGTH_LONG).show()
                }
            }

            override fun onError(call: Call?, response: Response?, e: Exception?) {
                LogUtil.e("个人信息获取失败-->${e!!.message}")
            }
        })
    }
}